#include "PlayerController.h"
using namespace  Branch1;
template<> PlayerController* DAISY::Tools::Singleton<PlayerController>::_singleton = new PlayerController();

PlayerController::PlayerController(){}

PlayerController::~PlayerController(){}

bool PlayerController::init(){return true;}

void PlayerController::shutDown(){}

void PlayerController::addTime(float interval)
{
	PlayerStateMap::const_iterator it;
	for(it = _stateRoot.begin();it != _stateRoot.end();it++)
	{
		it->second->addTime(interval);
	}
}
void PlayerController::injectKeyUp(const DAISY::KeyEvent & evt)
{
	PlayerStateMap::const_iterator it;
	for(it = _stateRoot.begin();it != _stateRoot.end();it++)
	{
		it->second->injectKeyUp(evt);
	}
}
void PlayerController::injectKeyDown(const DAISY::KeyEvent & evt)
{
	PlayerStateMap::const_iterator it;
	for(it = _stateRoot.begin();it != _stateRoot.end();it++)
	{
		it->second->injectKeyDown(evt);
	}
}
void PlayerController::injectMouseMove(const DAISY::MouseEvent & evt)
{
	PlayerStateMap::const_iterator it;
	for(it = _stateRoot.begin();it != _stateRoot.end();it++)
	{
		it->second->injectMouseMove(evt);
	}
}
void PlayerController::injectMouseUp(const DAISY::MouseEvent& evt, DAISY::MouseButtonID id)
{
	PlayerStateMap::const_iterator it;
	for(it = _stateRoot.begin();it != _stateRoot.end();it++)
	{
		it->second->injectMouseUp(evt,id);
	}
}
void PlayerController::injectMouseDown(const DAISY::MouseEvent& evt, DAISY::MouseButtonID id)
{
	PlayerStateMap::const_iterator it;
	for(it = _stateRoot.begin();it != _stateRoot.end();it++)
	{
		it->second->injectMouseDown(evt,id);
	}
}

void PlayerController::addStateToPool(const std::string& streamName,const std::string& stateName,PlayerState* state)
{
	boost::shared_ptr<PlayerState> statePtr = boost::shared_ptr<PlayerState>(state);
	
	(_statePool[streamName])[stateName] = statePtr;
}

void PlayerController::moveStateToRoot(const std::string& streamName,const std::string& targetStateName)
{
	boost::shared_ptr<PlayerState> newState = (_statePool[streamName])[targetStateName];
	newState->stateStart();
	_stateRoot [streamName] = newState;
}

void PlayerController::transitState(const std::string& streamName,const std::string& targetStateName)
{
	_stateRoot [streamName]->stateEnd();

	boost::shared_ptr<PlayerState> statePtr = (_statePool[streamName])[targetStateName];

	statePtr->stateStart();

	_stateRoot [streamName] = statePtr;
}